﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace IPlugin
{
    public class Request
    {
        //Attribute
        private Socket socket;
        private bool validRequest;

        //Properties
        public Dictionary<string, string> Header { get; private set; }
        public string Method { get; private set; }
        public RequestUrl ReqUrl { get; private set; }
        public RequestPostVariables ReqPostVars { get; private set; }

        public Request()
        {
            //nur fuer Testzwecke (z.B. UnitTests) implementiert
            Header = new Dictionary<string, string>();
        }
        public Request(Socket socket)
        {
            this.socket = socket;
            Method = "";
            Header = new Dictionary<string,string>();
            validRequest = false;
        }

        public bool Parse()
        {
            NetworkStream stream = new NetworkStream(socket);
            StreamReader reader = new StreamReader(stream);

            Console.WriteLine("-- Anfang --");

            while (!reader.EndOfStream)
            {
                string s1 = reader.ReadLine();
                Console.WriteLine(s1);
                string[] sary;

                if (string.IsNullOrEmpty(s1))
                    break;
                
                if (Method.Equals(""))
                {
                    //Erste Zeile z.B: GET /home.html HTTP/1.1
                    sary = s1.Split(' ');

                    if (sary.Length != 3)
                        return false;

                    Method = sary[0];
                    ReqUrl = new RequestUrl(sary[1]);
                    validRequest = true;
                }
                else
                {
                    //Anderen Zeilen des Headers, Host: .. User-Agent: .. Connection: ..
                    sary = s1.Split(new[] { ':' }, 2);

                    if (sary.Length != 2)
                    {
                        return false;
                    }

                    Header.Add(sary[0], sary[1]);
                }
                
            }

            //Es gibt moeglicherweise POST-Parameter
            try
            {
                if (Method.ToUpper().Equals("POST"))
                {
                    int postLenght = Convert.ToInt32(Header["Content-Length"]);
                    Console.WriteLine(postLenght);
                    char[] buffer = new char[postLenght];
                    reader.Read(buffer, 0, postLenght);
                    string s = new string(buffer);
                    ReqPostVars = new RequestPostVariables(s);
                }
            }
            catch (FormatException)
            {
                Console.WriteLine("Error: Die Content-Length der POST-Variablen konnte nicht in Integer umgewandel werden!");
            }
            catch (OverflowException)
            {
                Console.WriteLine("Error: Die Content-Length der POST-Variablen ist groesser als ein Int32!");
            }
            
            Console.WriteLine("-- Ende --");

            if (!validRequest)
                return false;

            return true;
        }
        public Socket getSocket()
        {
            return socket;
        }
    }
}
